package com.lxp.tksys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxp.tksys.common.R;
import com.lxp.tksys.dto.SetmealDto;
import com.lxp.tksys.entity.Category;
import com.lxp.tksys.entity.Employee;
import com.lxp.tksys.entity.Setmeal;
import com.lxp.tksys.entity.SetmealDish;
import com.lxp.tksys.service.CategoryService;
import com.lxp.tksys.service.SetmealDishService;
import com.lxp.tksys.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping//新增套餐
    @CacheEvict(value = "setMealCache",allEntries = true)        //springCache缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);          //调用自定义方法传入setmealDto
        return R.success("新增套餐成功");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);

        Page<Setmeal> pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getName).orderByDesc(Setmeal::getPrice);

        setmealService.page(pageInfo,queryWrapper);     //套餐setmeal根据name查询----还要显示套餐名称

        //dto拷贝获取套餐分类,根据catogryid
        Page<SetmealDto> dtoPage=new Page<>();
        //records页面数据，泛型不一样所以忽略
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");    //recors除外的copy
        //将records封装到list中
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);          //将item中的数据拷贝到setmealDto
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象，需要CaregoryService对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){ //分类对象不为空
                String categoryName = category.getName();//分类名称
                setmealDto.setCategoryName(categoryName);//将名称->setmealDto
            }
            return setmealDto;
        }).collect(Collectors.toList());


        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @CacheEvict(value = "setMealCache",allEntries = true)   //删除缓存SPringCache
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除ids:{}",ids);
        setmealService.removeWithDish(ids);        //自定义方法removeWithDish
        return R.success("套餐数据删除成功");
    }

    @Cacheable(value = "setMealCache",key="#setmeal.categoryId+'_'+#setmeal.status")     //获取列表时，设置缓存
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //分类id和status相同时，回显对应的套餐
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }




}
